/**
 * <p>Title: AsteroidThread</p>
 * <p>Description: The thread which will represent the asteroid behavior in space</p>
 * @author Guilherme Mauro Germoglio
 */


import java.awt.*;
import javax.swing.*;

public class AsteroidThread
    extends Thread {

  // atributes
  private Asteroid asteroid;
  private JPanel panel;
  private boolean paused;

  // methods

  /**
   * AsteroidThread's constructor. It constructs.
   * @param panel The panel where the asteroid will be drawn
   * @param asteroid The asteroid which be drawn on the panel
   */
  public AsteroidThread(JPanel panel, Asteroid asteroid) {
    this.panel = panel;
    this.asteroid = asteroid;
    this.paused = false;
  }

  /**
   * Check if the thread is paused or not
   * @return If the thread is paused or not
   */
  public boolean isPaused() {
    return paused;
  }

  /**
   * Sets the value of paused atribute
   * @param isOrNot If the thread will or won't be paused
   */
  public void setPause(boolean isOrNot) {
    this.paused = isOrNot;
  }

  /**
   * Moves the asteroid
   * @param asteroid The asteroid that will be moved
   */
  private void moveAsteroid(Asteroid asteroid) {
    asteroid.set_X( (asteroid.getX() + asteroid.get_X_velocity()));
    asteroid.set_Y( (asteroid.getY() + asteroid.get_Y_velocity()));
    checkBounds(asteroid);
  }

  /**
   * Check if the asteroid is or not out of window's bound. If so, teleports it
   * to the other side
   * @param asteroid The asteroid
   */
  private void checkBounds(Asteroid asteroid) {
    Dimension d = panel.getSize();
    if (asteroid.getX() < -25) {
      asteroid.set_X(d.width + 25);
    }
    if (asteroid.getX() > d.width + 25) {
      asteroid.set_X( -25);
    }
    if (asteroid.getY() < -25) {
      asteroid.set_Y(d.height + 25);
    }
    if (asteroid.getY() > d.height + 25) {
      asteroid.set_Y( -25);
    }
  }

  /**
   * Coordinates the asteroid's movement
   */
  public void move() {
    Graphics g = panel.getGraphics();
    Polygon p = new Polygon();
    asteroid.setDirection(asteroid.getDirection() + Math.PI / 180);
    if (!paused) {
      moveAsteroid(asteroid);
    }
    showAsteroid(g, Color.BLACK, asteroid);
  }

  /**
   * Draws the asteroid on the window
   * @param graphic The Graphics that will draw the asteroid
   * @param color The asteroid's color
   * @param asteroid The asteroid
   */
  private void showAsteroid(Graphics graphic, Color color, Asteroid asteroid) {
    double x = asteroid.getX();
    double y = asteroid.getY();
    double direction = asteroid.getDirection();
    graphic.setColor(color);
    graphic.drawPolygon(asteroid.drawAsteroid(x, y, direction));
    try {
      Thread.sleep(41);
    }
    catch (InterruptedException e) {}
    graphic.setColor(panel.getBackground());
    graphic.drawPolygon(asteroid.drawAsteroid(x, y, direction));
    graphic.dispose();
  }

  /**
   * The thread runner - may I say this? :)
   */
  public void run() {
    try {
      while (!asteroid.wasHit()) {
        move();
        Thread.sleep(1);
      }
    }
    catch (InterruptedException e) {}
  }
}